/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package utils;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.vecmath.Point3f;

/**
 * Utility class providing various read methods operating on byte arrays.
 * 
 * @author luke
 */
public abstract class DataReader {
	public static final int INT_SIZE = 4;
	public static final int SHORT_SIZE = 2;
	public static final int FLOAT_SIZE = 4;

	/**
	 * Reads a 4-byte little-endian int from {@code data} at offset
	 * {@code offset}.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @return the int at {@code offset} in {@code data}.
	 */
	public static int readInt(byte[] data, int offset) {
		byte[] intBytes = new byte[INT_SIZE];
		System.arraycopy(data, offset, intBytes, 0, INT_SIZE);
		return ByteBuffer.wrap(intBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	}

	/**
	 * Reads a 4-byte little-endian int from {@code raf}.
	 * 
	 * @param raf the RandomAccessFile to read from.
	 * @return the next int in this RandomAccessFile.
	 * @throws IOException if a file error occurs.
	 */
	public static int readInt(RandomAccessFile raf) throws IOException {
		byte[] intBytes = new byte[INT_SIZE];
		raf.read(intBytes, 0, INT_SIZE);
		return ByteBuffer.wrap(intBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	}

	/**
	 * Reads a 2-byte little-endian short from {@code data} at offset
	 * {@code offset}.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @return the short at {@code offset} in {@code data}.
	 */
	public static int readShort(byte[] data, int offset) {
		byte[] shortBytes = new byte[SHORT_SIZE];
		System.arraycopy(data, offset, shortBytes, 0, SHORT_SIZE);
		return ByteBuffer.wrap(shortBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
	}

	/**
	 * Reads a 4-byte little-endian float from {@code data} at offset
	 * {@code offset}.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @return the short at {@code offset} in {@code data}.
	 */
	public static float readFloat(byte[] data, int offset) {
		byte[] floatBytes = new byte[FLOAT_SIZE];
		System.arraycopy(data, offset, floatBytes, 0, FLOAT_SIZE);
		return ByteBuffer.wrap(floatBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getFloat();
	}

	/**
	 * Reads a 1-byte char from {@code data} at offset {@code offset}.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @return the char at {@code offset} in {@code data}.
	 */
	public static int readChar(byte[] data, int offset) {
		return data[offset] & 0xFF;
	}

	/**
	 * Reads a 3D "point", that is, three consecutive floats representing
	 * coordinates in space.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @return a Point3f object representing the coordinates at {@code offset}
	 *         in {@code data}.
	 */
	public static Point3f readPoint3f(byte[] data, int offset) {
		float x = readFloat(data, offset);
		float y = readFloat(data, offset + 4);
		float z = readFloat(data, offset + 8);
		return new Point3f(x, y, z);
	}

	/**
	 * Reads a short array from a byte array containing pixel indices, that is,
	 * an array in which each byte indexes a palette color.
	 * 
	 * @param data the byte array to read from.
	 * @param offset the offset in {@code data} to read at.
	 * @param size the size of the indices array.
	 * @return a short array containing the indices.
	 */
	public static short[] readIndices(byte[] data, int offset, int size) {
		short[] indices = new short[size];
		for (int i = 0; i < size; i++) {
			indices[i] = (short) (data[offset + i] & 0xFF);
		}
		return indices;
	}

	/**
	 * Converts {@code integer} to a little-endian byte array.
	 * 
	 * @param integer the int to convert.
	 * @return the byte array corresponding to {@code integer}.
	 */
	public static byte[] getIntBytes(int integer) {
		return ByteBuffer.allocate(INT_SIZE).order(ByteOrder.LITTLE_ENDIAN).putInt(integer).array();
	}
}
